<?php

namespace app\api\controller;

use app\admin\model\Knowledge AS KnowledgeMdl;
use app\admin\model\Knowledgetype;
use app\admin\model\Articlecomment;
use app\admin\model\Articlelike;
use app\admin\model\Image;

class Knowledge extends BaseController
{

    /**
     * 首页
     */
    public function getKnowledgeHomeFrame()
    {
        $param = self::getHttpParam();

        // 农事专题
        $knowledgetypeList = Knowledgetype::limit(0, 7)->select();

        // 最新发布
        $knowledgeList = $this->getLastList();

        $data = [
            'knowledgetypeList' => $knowledgetypeList,
            'knowledgeList' => $knowledgeList
        ];

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

    /**
     * @return \think\response\Json
     * 农事知识列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        /*if (empty($param->typeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeId 不能为空');
        }*/
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [];
        $knowledgetype = '';
        if(!empty($param->typeId)) {  //根据专题分类查询
            $where['typeId'] = $param->typeId;
            $knowledgetype = Knowledgetype::where(['id' => $param->typeId])->find();
        }

        if(!empty($param->search)) {  //根据关键字搜索
            $where['title'] = ['like','%'.$param->search.'%'];
        }

        $list = KnowledgeMdl::where($where)->order('topTime DESC,sorting ASC,createDate DESC')->limit($start, $length)->select();
        $total = KnowledgeMdl::where($where)->count();
        
        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到农事知识');
        }
        foreach ($list as $k => $a) {
            $list[$k]['imgUrl'] = $a['imgUrl'].'?imageView2/1/w/330/h/220';
        }
        $data = [
            'knowledgetype' => $knowledgetype,
            'list' => $list
        ];

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data, $total);
    }

    //获取最新发布的6条
    public function getLastList() {
        $list = KnowledgeMdl::order('topTime DESC,createDate DESC')->limit(0, 6)->select();
        foreach ($list as $k => $a) {
            $list[$k]['imgUrl'] = $a['imgUrl'].'?imageView2/1/w/330/h/220';
        }

        return $list;
    }

    //农事知识专题列表
    public function getType()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $typeList = Knowledgetype::limit($start, $length)->order('sorting ASC')->select();
        if (empty($typeList)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到分类信息');
        }
        foreach ($typeList as $k => $v) {
            $typeList[$k]['count'] = KnowledgeMdl::where('typeId',$v['id'])->count();
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $typeList);
    }

    /**
     * 农事知识详情
     */
    public function getDetail()
    {
        $param = self::getHttpParam();
        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id不能为空');
        }
        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }

        $articleId = $param->id;
        $item = KnowledgeMdl::where('id', $param->id)->find();
        if (empty($item)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到文章信息');
        }

        // 是否已收藏
        $collectWhere = [
            'userId' => $param->userId,
            'collectId' => $articleId,
            'type' => 3
        ];
        $collectInfo = action('api/Common/getCollectInfo', ['where' => $collectWhere]);
        $isCollect = 2;
        if($collectInfo){
            $isCollect = 1;
        }
        $item['isCollect'] = $isCollect;
        // 是否已点赞
        $is_praise = Articlelike::where(['articleId' => $articleId, 'userId' => $param->userId])->find();
        $item['is_praise'] = $is_praise ? 'true' : 'false';

        // 每查看一次，阅读数量加1
        KnowledgeMdl::where('id', $param->id)->setInc('readCount');

//        $item['readCount'] += 1;
//        $item['commentCount'] = Articlecomment::where('articleId',$param->id)->count();
//        $item['likeCount'] = Articlelike::where('articleId',$param->id)->count();
        $item['image'] = Image::where(['relatedId' => $param->id, 'tag' => 'imglist'])->order('sorting ASC')->select();

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

}